package gdata;

import bluecove.BlueSync;
import com.google.gdata.client.GoogleService;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.data.OutOfLineContent;
import com.google.gdata.data.TextContent;
import config.ConsoleUtil;
import config.SimpleProperties;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * Class description...
 *
 * @author mvu
 */
public class MyNoteBook {
  public static void main(String[] args) throws Exception {
    URL allNotebooks =
            new URL("http://www.google.com/notebook/feeds/05365436891260101842");
    GoogleService myService = new GoogleService("notebook", "example");

    Feed noteBooksFeed = myService.getFeed(allNotebooks, Feed.class);
    Map<String, String> noteBooks = new HashMap<String, String>();
    int i = 0;

    SimpleProperties myProperties = new SimpleProperties();
    long lastUpdated = myProperties.getNotebookLastUpdated();
    for (Entry entry : noteBooksFeed.getEntries()) {
      if (entry.getUpdated().getValue() > lastUpdated) {
        String uri = ((OutOfLineContent) entry.getContent()).getUri();
        URL singleNotebook = new URL(uri);
        Feed noteBookFeed = myService.getFeed(singleNotebook, Feed.class);
        String title = noteBookFeed.getTitle().getPlainText();
        StringBuffer html = new StringBuffer();
        html.append("<html>");
        for (Entry note : noteBookFeed.getEntries()) {
          String content = ((TextContent) note.getContent()).getContent().getPlainText();
          html.append("<pre style=\"color:")
                  .append(ConsoleUtil.getNextColor(i++))
                  .append(";\"><b>")
                  .append(content).append("</b></pre>");
        }
        html.append("</html>");
        noteBooks.put(title, html.toString());
      }
    }
    BlueSync.send(noteBooks);
    myProperties.setNotebookLastUpdated(System.currentTimeMillis());
    ConsoleUtil.end();
  }
}
